package com.future.mayi.fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.future.baselib.utils.ApiCst;
import com.future.baselib.utils.HttpRequest;
import com.future.mayi.R;
import com.future.mayi.activity.BaseFragment;
import com.future.mayi.activity.GoodsListActivity;
import com.future.mayi.adapter.CateAdapter;
import com.future.mayi.model.CateModel;
import com.future.mayi.model.SearchInfoModel;
import com.future.mayi.response.CateListResponse;
import com.future.mayi.response.TypeListResponse;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class StyleFragment extends BaseFragment implements AdapterView.OnItemClickListener {


    @BindView(R.id.ll_select)
    LinearLayout llSelect;
    @BindView(R.id.grid_view)
    GridView gridView;

    private LayoutInflater mInflater;
    private CateAdapter adapter;


    private List<TextView> textViewList;
    private List<View> selectViewList;

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_style;
    }

    @Override
    protected void initView() {
        mInflater = LayoutInflater.from(getContext());
        adapter = new CateAdapter(getContext(), R.layout.layout_item_brand, new ArrayList<CateModel>());
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(this);

    }

    @Override
    protected void initListener() {

    }


    private List<CateModel> cateList;
    private CateModel selectCateModel;

    @Override
    protected void initData() {
        if (cateList == null) {
            new HttpRequest<TypeListResponse>()
                    .with(getActivity())
                    .setApiCode(ApiCst.getSearchInfo)
                    .setListener(new HttpRequest.OnNetworkListener<TypeListResponse>() {
                        @Override
                        public void onSuccess(TypeListResponse response) {
                            SearchInfoModel searchInfoModel = response.model;
                            cateList = searchInfoModel.getCate_list();
                            initData();
                        }

                        @Override
                        public void onFail(String message, TypeListResponse response) {

                        }
                    })
                    .start(new TypeListResponse());
            return;
        }
        String pid = null;
        for (CateModel cateModel : cateList) {
            if (cateModel == null) continue;
            if (cateModel.getList() != null) continue;
            selectCateModel = cateModel;
            pid = String.valueOf(cateModel.getCate_id());
        }
        if (pid == null) {
            if (cateList == null || cateList.size() == 0) return;
            if (textViewList != null && textViewList.size() > 0) textViewList.clear();
            if (selectViewList != null && selectViewList.size() > 0) selectViewList.clear();
            llSelect.removeAllViews();
            textViewList = new ArrayList<>(cateList.size());
            selectViewList = new ArrayList<>(cateList.size());
            for (int i = 0; i < cateList.size(); i++) {
                CateModel model = cateList.get(i);
                View view = mInflater.inflate(R.layout.layout_style_tv_left_item, llSelect, false);
                llSelect.addView(view);
                view.setTag(i);
                TextView textView = (TextView) view.findViewById(R.id.tv_name);
                View viewSelect = view.findViewById(R.id.view_select);
                textViewList.add(textView);
                selectViewList.add(viewSelect);
                textView.setText(model.getCate_name());
                view.setOnClickListener(this);
            }
            setSelectPosition(0);
            return;
        }
        new HttpRequest<CateListResponse>()
                .with(getActivity())
                .addParam("pid", pid)
                .setApiCode(ApiCst.cateList)
                .setListener(new HttpRequest.OnNetworkListener<CateListResponse>() {
                    @Override
                    public void onSuccess(CateListResponse response) {
                        selectCateModel.setList(response.getCateList());
                        initData();
                    }

                    @Override
                    public void onFail(String message, CateListResponse response) {

                    }
                })
                .start(new CateListResponse());
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        CateModel model = (CateModel) parent.getAdapter().getItem(position);
        if (model == null) return;
        GoodsListActivity.startActivity(getActivity(), GoodsListActivity.StyleStyle, model.getCate_id());
    }


    @Override
    public void onClick(View v) {
        if (v.getTag() == null) return;
        if (!(v.getTag() instanceof Integer)) return;
        int position = (int) v.getTag();
        setSelectPosition(position);
    }

    private int selectPosition = -1;

    private void setSelectPosition(int position) {
        if (position == selectPosition) return;

        this.selectPosition = position;
        for (int i = 0; i < textViewList.size(); i++) {
            textViewList.get(i).setBackgroundColor(getResources().getColor(R.color.white));
            textViewList.get(i).setTextColor(getResources().getColor(R.color.text_color3));
            selectViewList.get(i).setVisibility(View.GONE);
        }

        textViewList.get(position).setBackgroundColor(getResources().getColor(R.color.bg_F6));
        textViewList.get(position).setTextColor(getResources().getColor(R.color.main_color));
        selectViewList.get(position).setVisibility(View.VISIBLE);
        adapter.updateList(cateList.get(position).getList());
        gridView.smoothScrollToPosition(0);
    }

}
